package by.jabbby.ui.act;

import android.content.ContextWrapper;
import android.content.Intent;
import android.util.Log;
import android.view.MenuItem;
import by.jabbby.Logger;
import by.jabbby.service.JabberServiceProxy;
import by.jabbby.service.Status;

public class MainMenuUtil {
	public static final int[] mainMenuItems = { R.id.mmi_account, R.id.mmi_exit, R.id.mmi_rosters, R.id.mmi_status };
	// private IJabberService jabberService;

	/*
	 * private ServiceConnection scChangeStatus = new ServiceConnection() {
	 * 
	 * public void onServiceDisconnected(ComponentName name) { jabberService =
	 * null;
	 * 
	 * }
	 * 
	 * public void onServiceConnected(ComponentName name, IBinder service) {
	 * jabberService = IJabberService.Stub.asInterface(service); //
	 * mJabberService.b try { jabberService.changeStatus(Status.OnLine.name());
	 * } catch (RemoteException e) { Log.e("", "", e); }
	 * 
	 * } };
	 */
	private JabberServiceProxy jabberProxy;
	private ContextWrapper cw;

	public MainMenuUtil(ContextWrapper cw, JabberServiceProxy jabberProxy) {
		this.cw = cw;
		this.jabberProxy = jabberProxy;
	}

	public void onMenuItemSelected(int featureId, MenuItem item) {
		try {
			switch (item.getItemId()) {

			case R.id.mmi_account:
				Intent settingsActivity = new Intent(cw.getBaseContext(), AccauntPreference.class);
				cw.startActivity(settingsActivity);
				break;

			case R.id.mmi_online:
				jabberProxy.getService().changeStatus(Status.OnLine.name());

				// serviceConnection.onServiceConnected(name, service)
				/*
				 * if ((jabberService != null) || (!activity.bindService(new
				 * Intent(activity.getApplicationContext(),
				 * JabberService.class), scChangeStatus,
				 * Context.BIND_AUTO_CREATE))) {
				 * 
				 * jabberService.changeStatus(Status.OnLine.name());
				 * Logger.e("jabby", "error when bind to the"); }
				 */
				break;

			case R.id.mmi_offline:

				// jabberProxy.getService().changeStatus(Status.OfLine.name());
				jabberProxy.getService().changeStatus(Status.OfLine.name());

				break;
			case R.id.mmi_rosters:
				Intent intent = new Intent(cw.getBaseContext(), Roster.class);
				cw.startActivity(intent);
				break;
			case R.id.mmi_exit:
				Log.i("myid", "terminated!!");
				jabberProxy.getService().exit();
				// Intent exitIntent = new Intent(cw.getBaseContext(),
				// Primary.class);
				// exitIntent.putExtra("exit", true);
				// cw.startActivity(exitIntent);
				break;
			// android.os.
			// item.onDestroy();
			// this.finish();
			// item.

			}
		} catch (Exception e) {
			Logger.e("onMenuItemSelected", e);
		}
	}
}
